package com.webkaifa.test;

import java.io.UnsupportedEncodingException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

import org.apache.log4j.Logger;

import com.webkaifa.utility.Utils;

public class RmiImNotifyResPacket extends RmiImPacketHeader{
	static Logger logger = Logger.getLogger(LogTest.class);
	public boolean m_bAutoFillFlag=false;
	public int m_nType=0;
	public byte[] m_szId=new byte[21];
	public int m_nContentType=0;
	public int m_nRelation=0;
	public int m_nContentId=0;
	public int m_nPositon=0;
	public int m_nSize=0;
	public int m_nTrans=0;
	public int m_nDelay=0;
	public String m_oUrl="";
	private void autoFillField()
	{
		if(m_bAutoFillFlag==true)
			return;
		m_bAutoFillFlag=true;
		m_nMsgType=ImNetMsgCode.IM_NOTIFY_RESP;
		m_nLength = super.headLength();
		try {
			m_nLength+=4+21+4*7+(m_oUrl+"\0").getBytes("GBK").length;
		} catch (UnsupportedEncodingException e) {
			logger.error(e);
		}
	}
	public int packetLength()
	{
		autoFillField();
		return super.packetLength();
	}
	public RmiImNotifyResPacket()
	{
		m_nMsgType=ImNetMsgCode.IM_NOTIFY_RESP;
	}
	public void setId(String id)
	{
		try {
			System.arraycopy(id.getBytes("GBK"),0,m_szId,0,id.getBytes("GBK").length);
		} catch (UnsupportedEncodingException e) {
			logger.error(e);
		}
	}
	public  int packetRead(ByteBuffer buffer)
	{
		try
		{
			super.packetRead(buffer);
			m_nType=buffer.getInt();
			buffer.get(m_szId);
			m_nContentType=buffer.getInt();
			m_nRelation=buffer.getInt();
			m_nContentId=buffer.getInt();
			m_nPositon=buffer.getInt();
			m_nSize=buffer.getInt();
			m_nTrans=buffer.getInt();
			m_nDelay=buffer.getInt();
			int l_nUrlLen=m_nLength-69;
			byte[] l_szByte=new byte[l_nUrlLen];
			buffer.get(l_szByte);
			m_oUrl=new String(l_szByte);
		}
		catch(BufferUnderflowException  e)
		{
			logger.error(Utils.getStackTraceAsString(e));
			return -1;
		}
		return 0;
	}
	public  int packetWrite(ByteBuffer buffer)
	{
		try
		{
			autoFillField();
			super.packetWrite(buffer);
			buffer.putInt(m_nType);
			buffer.put(m_szId);
			buffer.putInt(m_nContentType);
			buffer.putInt(m_nRelation);
			buffer.putInt(m_nContentId);
			buffer.putInt(m_nPositon);
			buffer.putInt(m_nSize);
			buffer.putInt(m_nTrans);
			buffer.putInt(m_nDelay);
			buffer.put((m_oUrl+"\0").getBytes("GBK"));
		}
		catch(BufferUnderflowException  e)
		{
			logger.error(Utils.getStackTraceAsString(e));
			return -1;
		} catch (UnsupportedEncodingException e) {
			logger.error(Utils.getStackTraceAsString(e));
			return -1;
		}
		return 0;
	}
}
